<?php
namespace app\index\controller;

use app\common\model\Seed as SeedModel;
use app\common\model\Fruit as FruitModel;
use app\common\model\SeedingFirst as SeedingFirstModel;
use app\common\component\seeding\SeedingFirstService;

class SeedingFirst extends ControllerBase {
	public function _initialize() {
		$this -> checkLogin();
	}

    public function create() {
        $post = $this -> request -> post();

		$userId = \think\Session::get('user_id');
		$currentTime = time();

		$seedingFirstModelGet = SeedingFirstModel::where('user_id = ' . $userId . ' AND sort = ' . $post['sort'] . ' AND status in(3, 4) ') -> find();
		if($seedingFirstModelGet) {
			return [ 'code' => -1, 'message' => '此地已经种植果子了' ];
		}

		$post['user_id'] = $userId;
		$post['start_time'] = $currentTime;
		$post['end_time'] = $post['start_time'] + getFarmParam('SEED_FIRST_GROW_UP_DAYS') * 86400;
		
		\think\Db::startTrans();
		try {
			$seedingCreate = (new SeedingFirstService()) -> create($post);
			if($seedingCreate['code'] != 0) {
				throw new \Exception($seedingCreate['message']);
			}

			if(!SeedModel::where('user_id', $userId) -> setDec('seed_first', $post['price'])) {
				throw new \Exception('播种失败, seed setDec!');
			}

			$seedLogCreate = (new \app\common\component\seed\SeedFirstLogService()) -> create([
				'user_id' => $userId,
				'type' => 2,
				'active' => 0,
				'price' => $post['price'],
				'create_time' => $currentTime,
				'remark' => '播种'
			]);
			if($seedLogCreate['code'] != 0) {
				throw new \Exception($seedLogCreate['message']);
			}

			\think\Db::commit();
		} catch(\Exception $e) {
			\think\Db::rollback();

			return ['code' => -1, 'message' => $e -> getMessage()];
		}

		return ['code' => 0, 'message' => '成功'];
    }

	public function addSeed() {
		$post = $this -> request -> post();

		$userId = \think\Session::get('user_id');

		$seedingFirstModelGet = SeedingFirstModel::where('user_id = ' . $userId . ' AND sort = ' . $post['sort'] . ' AND status = 3 ') -> find();
		if(!$seedingFirstModelGet) {
			return [ 'code' => -1, 'message' => '此地没有果子/或果子等待收获中!' ];
		}

		if($post['sort'] < 11) {
			if($seedingFirstModelGet['price'] + $post['price'] > 3000) {
				return [ 'code' => -1, 'message' => '添加的种子超过指定数目!' ];
			}
		} else {
			if($seedingFirstModelGet['price'] + $post['price'] > 30000) {
				return [ 'code' => -1, 'message' => '添加的种子超过指定数目!' ];
			}
		}

		\think\Db::startTrans();
		try {
			$seedingFirstModelGet -> price += $post['price'];
			if(!$seedingFirstModelGet -> save()) {
				throw new \Exception('添加种子失败, save');
			}

			if(!SeedModel::where('user_id', $userId) -> setDec('seed_first', $post['price'])) {
				throw new \Exception('播种失败, seed setDec!');
			}

			\think\Db::commit();
		} catch(\Exception $e) {
			\think\Db::rollback();

			return ['code' => -1, 'message' => $e -> getMessage()];
		}

		return ['code' => 0, 'message' => '成功'];
	}

	public function getSeed() {
		$post = $this -> request -> post();

		$userId = \think\Session::get('user_id');

		$seedingFirstModelGet = SeedingFirstModel::where('user_id = ' . $userId . ' AND sort = ' . $post['sort'] . ' AND status = 4 ') -> find();
		if(!$seedingFirstModelGet) {
			return [ 'code' => -1, 'message' => '果子未成熟!' ];
		}

		\think\Db::startTrans();
		try {
			$seedingFirstModelGet -> status = 9;
			if(!$seedingFirstModelGet -> save()) {
				throw new \Exception('收获果子失败, save');
			}
			
			$price = $seedingFirstModelGet -> price + $seedingFirstModelGet -> profit;
			if(!FruitModel::where('user_id', $userId) -> setInc('fruit_first', $price)) {
				throw new \Exception('收获果子失败, fruit setInc!');
			}
			
			$fruitFirstLogServiceCreate = (new \app\common\component\fruit\FruitFirstLogService()) -> create([
				'user_id' => $userId,
				'type' => 2,
				'active' => 1,
				'price' => $price,
				'create_time' => time(),
				'remark' => '收获第' . $seedingFirstModelGet['sort'] . '地的果子'
			]);
			if($fruitFirstLogServiceCreate['code'] != 0) {
				throw new \Exception($fruitFirstLogServiceCreate['message']);
			}

			\think\Db::commit();
		} catch(\Exception $e) {
			\think\Db::rollback();

			return ['code' => -1, 'message' => $e -> getMessage()];
		}

		return ['code' => 0, 'message' => '成功'];
	}
}
?>